package com.iraeta.service.etl;

import com.iraeta.dao.erp.*;
import com.iraeta.dao.main.*;
import com.iraeta.entity.main.*;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ErpEtlService {
    @Autowired
    EtllogDao etllogDao;
    @Autowired
    SoMainDao soMainDao;
    @Autowired
    ErpSomainDao erpSomainDao;
    @Autowired
    RdRecordScDao rdRecordScDao;
    @Autowired
    ErpRdrecordScDao erpRdrecordScDao;
    @Autowired
    MomOrderDao momOrderDao;
    @Autowired
    ErpMomOrderDetailDao erpMomOrderDetailDao;
    @Autowired
    RdRecord32Dao rdRecord32Dao;
    @Autowired
    ErpRdrecord32Dao erpRdrecord32Dao;
    @Autowired
    RdRecord01WXDao rdRecord01WXDao;
    @Autowired
    ErpRdrecord01WxDao erpRdrecord01WxDao;
    @Autowired
    RdRecord11Dao rdRecord11Dao;
    @Autowired
    ErpRdrecord11Dao erpRdrecord11Dao;

    public void processErpSoMain(){
        List<HashMap<String, Object>> res = soMainDao.listSoMain();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                ErpSomain item = new ErpSomain();
                Util.mapToBean(map, item);
                ErpSomain erpSomainDb = erpSomainDao.selectByPrimaryKey(item.getAutoid());
                if(erpSomainDb == null){
                    erpSomainDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpSomainDb)){
                        erpSomainDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        int deleteCount = 0;
        try {
            List<Integer> erpIdList = res.stream().map(x->(Integer)x.get("AutoID")).collect(Collectors.toList());
            List<ErpSomain> erpSomainList = erpSomainDao.selectTopRecord();
            List<ErpSomain> filterErpSomainList = erpSomainList.stream().filter(x->  !erpIdList.contains(x.getAutoid().intValue()) ).collect(Collectors.toList());
            deleteCount = filterErpSomainList.size();
            for (ErpSomain erpSomain : filterErpSomainList) {
                erpSomainDao.deleteByPrimaryKey(erpSomain.getAutoid());
            }
        }catch (Exception e){
            exceptionCount++;
        }
        Etllog etllog = new Etllog("ERP销售订单", insertCount, updateCount, exceptionCount, deleteCount);
        etllogDao.insert(etllog);
        System.out.println("ERP销售订单新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + deleteCount);
    }

    public void processErpRdrecordSc(){
        List<HashMap<String, Object>> res = rdRecordScDao.listRdRecordSc();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                ErpRdrecordSc item = new ErpRdrecordSc();
                Util.mapToBean(map, item);
                ErpRdrecordSc erpRdrecordScDb = erpRdrecordScDao.selectByPrimaryKey(item.getAutoid());
                if(erpRdrecordScDb == null){
                    erpRdrecordScDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpRdrecordScDb)){
                        erpRdrecordScDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        int deleteCount = 0;
        try {
            List<Long> erpIdList = res.stream().map(x->(Long)x.get("AutoID")).collect(Collectors.toList());
            List<ErpRdrecordSc> erpRdrecordScList = erpRdrecordScDao.selectTopRecord();
            List<ErpRdrecordSc> filterErpRdrecordScList = erpRdrecordScList.stream().filter(x->  !erpIdList.contains(x.getAutoid()) ).collect(Collectors.toList());
            deleteCount = filterErpRdrecordScList.size();
            for (ErpRdrecordSc erpRdrecordSc : filterErpRdrecordScList) {
                erpRdrecordScDao.deleteByPrimaryKey(erpRdrecordSc.getAutoid());
            }
        }catch (Exception e){
            exceptionCount++;
        }
        Etllog etllog = new Etllog("ERP产成品入库", insertCount, updateCount, exceptionCount, deleteCount);
        etllogDao.insert(etllog);
        System.out.println("ERP产成品入库新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + deleteCount);
    }

    public void processErpMomOrder(){
        List<HashMap<String, Object>> res = momOrderDao.listMom_OrderDetail();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                ErpMomOrderDetail item = new ErpMomOrderDetail();
                Util.mapToBean(map, item);
                ErpMomOrderDetail erpMomOrderDetailDb = erpMomOrderDetailDao.selectByPrimaryKey(item.getModid());
                if(erpMomOrderDetailDb == null){
                    erpMomOrderDetailDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpMomOrderDetailDb)){
                        erpMomOrderDetailDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        int deleteCount = 0;
        try {
            List<Integer> erpIdList = res.stream().map(x->(Integer)x.get("MoDId")).collect(Collectors.toList());
            List<ErpMomOrderDetail> erpMomOrderDetailList = erpMomOrderDetailDao.selectTopRecord();
            List<ErpMomOrderDetail> filterErpMomOrderDetailList = erpMomOrderDetailList.stream().filter(x->  !erpIdList.contains( x.getModid().intValue())).collect(Collectors.toList());
            deleteCount = filterErpMomOrderDetailList.size();
            for (ErpMomOrderDetail erpMomOrderDetail : filterErpMomOrderDetailList) {
                erpMomOrderDetailDao.deleteByPrimaryKey(erpMomOrderDetail.getModid());
            }
        }catch (Exception e){
            exceptionCount++;
        }
        Etllog etllog = new Etllog("ERP生产订单明细", insertCount, updateCount, exceptionCount, deleteCount);
        etllogDao.insert(etllog);
        System.out.println("ERP生产订单明细新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + deleteCount);
    }

    public void processErpRdrecord32(){
        List<HashMap<String, Object>> res = rdRecord32Dao.listRdRecord32();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                ErpRdrecord32 item = new ErpRdrecord32();
                Util.mapToBean(map, item);
                ErpRdrecord32 erpRdrecord32Db = erpRdrecord32Dao.selectByPrimaryKey(item.getAutoid());
                if(erpRdrecord32Db == null){
                    erpRdrecord32Dao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpRdrecord32Db)){
                        erpRdrecord32Dao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }

        //处理删除的数据
        int deleteCount = 0;
        try {
            List<Long> erpIdList = res.stream().map(x->(Long)x.get("AutoID")).collect(Collectors.toList());
            List<ErpRdrecord32> erpRdrecord32List = erpRdrecord32Dao.selectTopRecord();
            List<ErpRdrecord32> filterErpRdrecord32List = erpRdrecord32List.stream().filter(x->  !erpIdList.contains(x.getAutoid()) ).collect(Collectors.toList());
            deleteCount = filterErpRdrecord32List.size();
            for (ErpRdrecord32 erpRdrecord32 : filterErpRdrecord32List) {
                erpRdrecord32Dao.deleteByPrimaryKey(erpRdrecord32.getAutoid());
            }
        }catch (Exception e){
            exceptionCount++;
        }
        Etllog etllog = new Etllog("ERP销售出库单", insertCount, updateCount, exceptionCount, deleteCount);
        etllogDao.insert(etllog);
        System.out.println("ERP销售出库单新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + deleteCount);
    }

    public void processErpRdrecord01WX(){
        List<HashMap<String, Object>> res = rdRecord01WXDao.listRdRecord01WX();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                ErpRdrecord01Wx item = new ErpRdrecord01Wx();
                Util.mapToBean(map, item);
                ErpRdrecord01Wx erpRdrecord01WxDb = erpRdrecord01WxDao.selectByPrimaryKey(item.getAutoid());
                if(erpRdrecord01WxDb == null){
                    erpRdrecord01WxDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpRdrecord01WxDb)){
                        erpRdrecord01WxDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        int deleteCount = 0;
        try {
            List<Long> erpIdList = res.stream().map(x->(Long)x.get("AutoID")).collect(Collectors.toList());
            List<ErpRdrecord01Wx> erpRdrecord01WxList = erpRdrecord01WxDao.selectTopRecord();
            List<ErpRdrecord01Wx> filterErpRdrecord01WxList = erpRdrecord01WxList.stream().filter(x->  !erpIdList.contains(x.getAutoid()) ).collect(Collectors.toList());
            deleteCount = filterErpRdrecord01WxList.size();
            for (ErpRdrecord01Wx erpRdrecord01Wx : filterErpRdrecord01WxList) {
                erpRdrecord01WxDao.deleteByPrimaryKey(erpRdrecord01Wx.getAutoid());
            }
        }catch (Exception e){
            exceptionCount++;
        }
        Etllog etllog = new Etllog("ERP外协采购单", insertCount, updateCount, exceptionCount, deleteCount);
        etllogDao.insert(etllog);
        System.out.println("ERP外协采购单新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + deleteCount);
    }

    public void processErpRdrecord11(){
        List<HashMap<String, Object>> res = rdRecord11Dao.listRdRecord11();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                ErpRdrecord11 item = new ErpRdrecord11();
                Util.mapToBean(map, item);
                ErpRdrecord11 erpRdrecord11Db = erpRdrecord11Dao.selectByPrimaryKey(item.getAutoid());
                if(erpRdrecord11Db == null){
                    erpRdrecord11Dao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpRdrecord11Db)){
                        erpRdrecord11Dao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }

        //处理删除的数据
        int deleteCount = 0;
        try {
            List<Long> erpIdList = res.stream().map(x->(Long)x.get("AutoID")).collect(Collectors.toList());
            List<ErpRdrecord11> erpRdrecord11List = erpRdrecord11Dao.selectTopRecord();
            List<ErpRdrecord11> filterErpRdrecord11List = erpRdrecord11List.stream().filter(x->  !erpIdList.contains(x.getAutoid()) ).collect(Collectors.toList());
            deleteCount = filterErpRdrecord11List.size();
            for (ErpRdrecord11 erpRdrecord11 : filterErpRdrecord11List) {
                erpRdrecord11Dao.deleteByPrimaryKey(erpRdrecord11.getAutoid());
            }
        }catch (Exception e){
            exceptionCount++;
        }
        Etllog etllog = new Etllog("ERP材料出库单", insertCount, updateCount, exceptionCount, deleteCount);
        etllogDao.insert(etllog);
        System.out.println("ERP材料出库单新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + deleteCount);
    }
}
